package com.ai.trickycards.ui.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityOptionsCompat
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import com.ai.trickycards.App

open class BaseActivity : ComponentActivity() {

    protected val appViewModel by lazy {
        (application as App).getAppViewModel()
    }

    protected val displayMetrics: DisplayMetrics by lazy {
        resources.displayMetrics
    }

    private val activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        onActivityResult(result.data, result.data?.getIntExtra("launcherRequestCode", 0) ?: 0, result.resultCode)
    }

    open fun onActivityResult(intent: Intent?, requestCode: Int, resultCode: Int) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.dark(Color.TRANSPARENT),
            navigationBarStyle = SystemBarStyle.dark(Color.TRANSPARENT),
        )
    }

    protected fun setOnApplyWindowInsetsListener(
        root: View,
        listener: (systemBars: Insets) -> Unit
    ) {
        ViewCompat.setOnApplyWindowInsetsListener(root) { _, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            listener.invoke(systemBars)
            insets
        }
    }

    protected fun setWindowInsets(
        root: View,
        insetTop: Boolean = true,
        insetBottom: Boolean = true
    ) {
        setOnApplyWindowInsetsListener(root) { systemBars ->
            root.updatePadding(
                top = if (insetTop) systemBars.top else 0,
                bottom = if (insetBottom) systemBars.bottom else 0
            )
        }
    }

    fun <T> openActivity(cls: Class<T>, args: Bundle?=null) {
        startActivity(Intent(this, cls).apply {
            if(args != null) {
                this.putExtras(args)
            }
        })
    }

    fun <T> openActivityForResult(cls: Class<T>, requestCode: Int, options: ActivityOptionsCompat? = null) {
        openActivityForResult(Intent(this, cls), requestCode, options)
    }

    fun openActivityForResult(intent: Intent, requestCode: Int, options: ActivityOptionsCompat? = null) {
        intent.putExtra("launcherRequestCode", requestCode)
        activityLauncher.launch(intent, options)
    }

    fun finish(resultCode: Int, data: Intent? = null) {
        val realData = data ?: Intent()
        realData.putExtra("launcherRequestCode", intent.getIntExtra("launcherRequestCode", 0))
        setResult(resultCode, realData)
        super.finish()
    }
}